package com.probiz.estoresa.system.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.product.ProductType;
import com.probiz.estore.common.model.system.TaxRate;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.product.service.ProductTypeManager;
import com.probiz.estore.system.service.RegionManager;
import com.probiz.estore.system.service.TaxManager;
import com.probiz.estore.system.service.TaxRateManager;


public class TaxRateController extends GenericStoreAdminController<TaxRate> {
    private TaxRateManager taxRateManager = null;
    private TaxManager taxManager=null;
	private ProductTypeManager	productTypeManager	= null;
	
	private RegionManager regionManager = null;

	public void setRegionManager(RegionManager regionManager) {
		this.regionManager = regionManager;
	}

	public void setProductTypeManager(ProductTypeManager productTypeManager) {
		this.productTypeManager = productTypeManager;
	}

    public void setTaxRateManager(TaxRateManager taxRateManager) {
        this.taxRateManager = taxRateManager;
    }
    
    public void setTaxManager(TaxManager taxManager){
    	this.taxManager=taxManager;
    }
    
    @Override
    public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) {
    	ModelAndView mv = search(request, response);
    	request.setAttribute(listModelName, (List<TaxRate>)mv.getModel().get(listModelName));
    	return mv;
	}
   
    /**
     * records delete
     * @param request
     * @param response
     */
    public ModelAndView multiDelete(HttpServletRequest request,
            HttpServletResponse response) {
        if (logger.isDebugEnabled()) {
            logger.debug("entering 'TaxRateController multiDelete' method...");
        }
        
        //get pkey array from request 
        String[] pkeys = request.getParameterValues("multiIds");
        if (pkeys != null) {
        	StringBuffer sbNames=new StringBuffer();
            for (int i = 0; i < pkeys.length; i++) {
                if (pkeys[i]!=null && !pkeys[i].equals("")) {
                	TaxRate taxRate = mgr.getById(new Integer(pkeys[i]));
                	ProductType productType = productTypeManager.getById(Integer.valueOf(taxRate.getProductTypeId()));
                    mgr.deleteById(new Integer(pkeys[i]));
                    if (logger.isDebugEnabled()) {
                        logger.debug("TaxRateController delete key =" + pkeys[i]);
                    }
                    //save message
                    sbNames.append(",").append(productType.getProductTypeName());
                }
            }
            if(sbNames.length()>0){
            	sbNames.delete(0, 1);
    	        String message = getMessage("taxRate.deleted",new Object[] {sbNames});
    	        saveMessage(request, message);
            }
        }
		
		return this.returnToSearch(request, response);
		
    }    
    
    
    protected TaxRate formBackingObject(HttpServletRequest request) {
		TaxRate taxRate = super.formBackingObject(request);
		request.setAttribute("taxs", taxManager.listAll());
		request.setAttribute("productTypeList", productTypeManager.listAll());
		return taxRate;
	}

	@Override
	protected String getEntityName(TaxRate entity) {
		return entity.getProductType().getProductTypeName()+entity.getTax().getTaxName();
	}


	@Override
	protected void initController() throws Exception {
		this.mgr = taxRateManager;
		
	}
	
	@Override
	protected Map<String,String> getSortMap(){
		Map<String,String> sortMap = new HashMap<String,String>();
		sortMap.put("1", "convert_gbk(tax.taxName)");
		sortMap.put("2", "convert_gbk(productType.productTypeName)");
		sortMap.put("4", "rateValue");
		return sortMap;
	}

	@Override
	protected void preSave(HttpServletRequest request, TaxRate entity, BindException errors) {
		if (taxRateManager.checkExist(entity)) {
			errors.reject("taxRate.updatedFailed");
		}
	}  
	
	@Override
	protected void postSave (HttpServletRequest request, TaxRate entity){
		//保存操作成功的提示信息
		String msgKey = (isEntityNew(request)) ? "taxRate.added"	: "taxRate.updated";
		String message = getMessage(msgKey);
		saveMessage(request, message);
	}
}
